import logging
from minio import Minio
from minio.error import S3Error
import datetime

import configparser
logging.basicConfig(level=logging.INFO)

cnf_file = "myback.ini"
config = configparser.ConfigParser()
config.read(cnf_file)
minio_host = config.get("minio", "minio_host")
access_key = config.get("minio", "access_key")
secret_key = config.get("minio", "secret_key")
project_code = config.get("minio", "project_code")

storage_path = config.get("download", "storage_path")
pull_start_at = config.get("download", "pull_start_at")
pull_end_at = config.get("download", "pull_end_at")

minioClient = Minio(
    minio_host,
    access_key,
    secret_key,
    secure=False
)

def bucket_list_files(bucket_name, prefix):
    try:
        need_pull_files = []
        files_list = minioClient.list_objects(bucket_name=bucket_name, prefix=prefix, recursive=True)
        for obj in files_list:
            if "backupfilelist"  in obj.object_name:
                need_pull_files.append(obj.object_name)
            else:
                bak_datetime = datetime.datetime.strptime(obj.object_name.replace("_full.tar.gz", "").replace("_incr.tar.gz", "").replace("mysql/",""), '%Y%m%d%H%M%S')
                pull_start_at_time = datetime.datetime.strptime(pull_start_at, '%Y-%m-%d %H:%M:%S')
                pull_end_at_time = datetime.datetime.strptime(pull_end_at, '%Y-%m-%d %H:%M:%S')
                if bak_datetime >=pull_start_at_time and bak_datetime <=pull_end_at_time:
                    need_pull_files.append(obj.object_name)
        print("符合时间要求的文件名列表:{0}".format(need_pull_files))
        return need_pull_files
    except S3Error as e:
        logging.error("list file failed:", e)

def pullOfMinio():
    try:
        need_pull_files = bucket_list_files("mysqlbackup",project_code)
        for filename in need_pull_files:
            minioClient.fget_object("mysqlbackup", filename, "{0}/{1}".format(storage_path,filename))
            logging.info("成功拉取文件：{}".format(filename))
    except BaseException as err:
        logging.error("拉取文件错误:", err)

if __name__ == '__main__':
    pullOfMinio()